<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="订单号" prop="orderNo">
          <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入订单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['longOrder:record:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="recordList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="账单编号"
          align="center"
          prop="billId"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="订单编号"
          align="center"
          prop="orderNo"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="应收款总金额"
          align="center"
          prop="accountAmount"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column label="已收总金额" align="center" prop="paidAmount" />
        <el-table-column
          label="账单周期"
          align="center"
          prop="billStartTime"
          min-width="200"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span
              >{{ parseTime(scope.row.billStartTime, '{y}-{m}-{d}') }} 至
              {{ parseTime(scope.row.billEndTime, '{y}-{m}-{d}') }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          label="还款日"
          align="center"
          prop="paymentDueDate"
          min-width="200"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.paymentDueDate, '{y}-{m}-{d}')
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="实际还款时间"
          align="center"
          prop="paidTime"
          min-width="200"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.paidTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="付款状态"
          align="center"
          prop="payStatus"
          min-width="100"
          show-overflow-tooltip
        >
          <template slot-scope="{ row }">
            <dict-tag
              :options="dict.type.order_payStatus"
              :value="row.payStatus"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="期数"
          align="center"
          prop="termsNo"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="180"
          fixed="left"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.payStatus == 1"
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['longOrder:record:edit']"
              >处理账单</el-button
            >
            <el-button
              v-else
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改长租账单记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row :gutter="15">
          <el-col :span="24">
            <MmvTitle title="账单信息"></MmvTitle>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单编号">{{ form.orderNo }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款状态">
              <dict-tag
                :options="dict.type.order_payStatus"
                :value="form.payStatus"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账单期数">第{{ form.termsNo }}期</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账单是否开始">
              <dict-tag
                :options="dict.type.business_yes_no"
                :value="form.isStart"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账单开始时间">{{
              form.billStartTime
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账单结束时间">{{
              form.billStartTime
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应收款总金额"
              >{{ form.accountAmount }}元</el-form-item
            >
          </el-col>
          <el-col :span="12">
            <el-form-item label="账单还款日">{{
              form.paymentDueDate
            }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <MmvTitle title="支付信息"></MmvTitle>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="已收总金额"
              prop="paidAmount"
              v-if="form.payStatus == 1"
            >
              <el-input
                style="width: 100%; margin-right: 15px"
                readonly
                v-model="form.paidAmount"
                type="number"
                placeholder="总金额"
              ></el-input>
            </el-form-item>
            <el-form-item label="已收总金额" v-else>{{
              form.paidAmount
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="实际还款时间"
              prop="paidTime"
              v-if="form.payStatus == 1"
            >
              <el-date-picker
                style="width: 100%; margin-right: 15px"
                v-model="form.paidTime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="还款时间"
                type="datetime"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="实际还款时间" v-else>{{
              form.paidTime
            }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="支付方式" required v-if="form.payStatus == 1">
              <el-radio-group v-model="payWay">
                <el-radio label="1">微信</el-radio>
                <el-radio label="2">支付宝</el-radio>
                <el-radio label="3">信用卡</el-radio>
                <el-radio label="4">现金</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="支付方式" v-else>{{
              form.payWay | payWay
            }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="支付凭证" required>
              <ImageUpload
                v-model="form.url"
                :limit="1"
                v-if="form.payStatus == 1"
              />
              <el-image
                :src="form.url | imageUrl"
                v-else
                :preview-src-list="[$mmvImageUrl([form.url])]"
                style="width: 160px; height: 90px; margin-right: 15px"
              ></el-image>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm"
          :loading="submitloading"
          v-if="form.payStatus == 1"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addRecord,
  delRecord,
  exportRecord,
  getRecord,
  listRecord,
  updateRecord,
} from '@/api/longOrder/record'
import MmvTitle from '@/components/MmvUI/MmvTitle.vue'
export default {
  name: 'Record',
  dicts: ['order_payStatus', 'business_yes_no'],
  components: {
    MmvTitle,
  },
  filters: {
    payWay(val) {
      return (
        {
          1: '微信',
          2: '支付宝',
          3: '信用卡',
          4: '现金',
        }[Number(val)] || ''
      )
    },
    imageUrl(url) {
      return process.env.VUE_APP_BASE_IMAGE_URL + url
    },
  },
  data() {
    return {
      submitloading: false,
      payWay: '1',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 长租账单记录表格数据
      recordList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billType: null,
        orderNo: null,
        accountAmount: null,
        paidAmount: null,
        billStartTime: null,
        billEndTime: null,
        paymentDueDate: null,
        paidTime: null,
        payStatus: null,
        termsNo: null,
        isStart: null,
        deptId: null,
        tenantId: null,
      },
      // 表单参数
      form: {
        paidTime: new Date(),
      },
      // 表单校验
      rules: {
        paidAmount: [
          {
            required: true,
            message: '支付金额不能为空',
            trigger: 'blur',
          },
        ],
        paidTime: [
          {
            required: true,
            message: '支付时间不能为空',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询长租账单记录列表 */
    getList() {
      this.loading = true
      listRecord(this.queryParams).then((response) => {
        this.recordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        billType: null,
        orderNo: null,
        accountAmount: null,
        paidAmount: null,
        billStartTime: null,
        billEndTime: null,
        paymentDueDate: null,
        paidTime: new Date(),
        payStatus: 0,
        termsNo: null,
        isStart: null,
        createBy: null,
        createTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
        updateBy: null,
        updateTime: null,
      }
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.billId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加长租账单记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const billId = row.billId || this.ids
      getRecord(billId).then((response) => {
        this.form = response.data
        this.form.paidTime = this.parseTime(new Date())
        this.form.paidAmount = this.form.accountAmount
        this.payWay = '1'
        this.open = true
        this.title = '长租账单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.submitloading = true
      setTimeout(() => {
        this.submitloading = false
      }, 1000)
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.billId != null) {
            let data = {
              ...this.form,
              payWay: this.payWay,
            }
            updateRecord(data).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRecord(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids
      this.$modal
        .confirm('是否确认删除长租账单记录编号为"' + billIds + '"的数据项？')
        .then(function () {
          return delRecord(billIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有长租账单记录数据项？')
        .then(() => {
          this.exportLoading = true
          return exportRecord(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
